package com.xxx.test;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;

/**
 * @author hdz
 * @date 2019/03/29
 */
public class AppManager {

    public AppManager() {

    }

    /**
     * 获取其它应用信息
     * @param context Context
     * @param packageName 应用包名
     */
    public AppInfo getAppInfo(Context context, String packageName) {
        // 获取包管理器
        PackageManager pm = context.getPackageManager();
        try {
            // 根据包名获取ApplicationInfo
            ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
            // 获取应用图标
            Drawable icon = appInfo.loadIcon(pm);
            Drawable appIcon = pm.getApplicationIcon(packageName);
            // 获取应用名称
            String label = appInfo.loadLabel(pm).toString();

            // 根据包名获取PackageInfo
            PackageInfo pkgInfo = pm.getPackageInfo(packageName, 0);
            // 获取应用版本号
            String versionName = pkgInfo.versionName;

            return new AppInfo(packageName, label, versionName, icon);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过包名跳转到其它应用
     * @param context Context
     * @param packageName 应用包名
     */
    public void goOtherApp(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(packageName);
        if (intent != null) {
            context.startActivity(intent);
        }
    }


    /**
     * 跳转到应用详情设置页
     * @param context Context
     * @param packageName 应用包名
     */
    public void goAppDetailSetting(Context context, String packageName){
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        intent.setData(Uri.fromParts("package", packageName, null));
        context.startActivity(intent);
    }
}
